package com.powernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.constant.ProductConstants;
import com.powernode.entity.Category;
import com.powernode.ex.handler.BusinessException;
import com.powernode.service.CategoryService;
import com.powernode.mapper.CategoryMapper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author yanju
* {@code @description} 针对表【category(产品类目)】的数据库操作Service实现
* {@code @createDate} 2024-11-06 10:20:05
 */
@Service
@CacheConfig(cacheNames = "com.powernode.service.impl.CategoryServiceImpl")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 查询系统商品所有类目
     * @return 商品类目集合List
     */
    @Override
    @Cacheable(key = ProductConstants.ALL_CATEGORY_LIST_KEY)
    public List<Category> queryAllCateforyList() {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .orderByDesc(Category::getSeq));
    }

    /**
     * 查询系统商品一级类目
     * @return 一级类目对象集合
     */
    @Override
    @Cacheable(key = ProductConstants.FIRST_CATEGOTY_LIST_KEY)
    public List<Category> queryFirstCategoryList() {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId,0)
                .eq(Category::getStatus,1)
                .orderByDesc(Category::getSeq));
    }

    /**
     * 新增商品类目
     * @param category 商品类目对象
     * @return 是否新增成功 true：成功
     */
    @Override
    @Caching(evict = {
            @CacheEvict(key = ProductConstants.ALL_CATEGORY_LIST_KEY),
            @CacheEvict(key = ProductConstants.FIRST_CATEGOTY_LIST_KEY)
    })
    public Boolean saveCategory(Category category) {
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        return categoryMapper.insert(category) > 0;
    }

    /**
     * 修改商品类目信息
     * @param category
     * @return
     */
    @Override
    @Caching(evict = {
            @CacheEvict(key = ProductConstants.ALL_CATEGORY_LIST_KEY),
            @CacheEvict(key = ProductConstants.FIRST_CATEGOTY_LIST_KEY)
    })
    public Boolean modifyCategory(Category category) {
        //1.根据标识查商品详情信息
        Category beforeCategory = categoryMapper.selectById(category.getCategoryId());
        //2.获取商品类目修改之前的级别
        Long beforeParentId = beforeCategory.getParentId();
        //3.获取修改后的pid
        Long parentId = category.getParentId();
        //4.判断商品类目修改详情
        //4.1.  1 -> 2 ：之前pid为0，修改后pid不为0
        if (beforeParentId ==0 && parentId != null && parentId != 0 ){
            //查询当前类目是否包含子类目，包含不允许修改
            //根据当前类目标查询子类目
            List<Category> childList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                    .eq(Category::getParentId, category.getParentId()));

            if (!childList.isEmpty()){
                throw new BusinessException("当前类目包含子类目，不可修改");
            }
        }

        //4.2.   2 -> 1：之前pid不为0， 并且当前pid为null
        if (beforeParentId != 0 && parentId == null){
            category.setParentId(0L);
        }
        return categoryMapper.updateById(category) > 0;
    }


    /**
     * 删除商品类目
     *  如果一级类目包含子类目，则不可删除
     * @param categoryId
     * @return
     */
    @Override
    @Caching(evict = {
            @CacheEvict(key = ProductConstants.ALL_CATEGORY_LIST_KEY),
            @CacheEvict(key = ProductConstants.FIRST_CATEGOTY_LIST_KEY)
    })
    public Boolean removeCategoryById(Long categoryId) {
        //根据类目标识查询子类目集合
        List<Category> childCategory = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, categoryId));
        if (!childCategory.isEmpty()){
            throw new BusinessException("当前类目包含子类目，不可删除");
        }

        return categoryMapper.deleteById(categoryId) > 0;
    }




}




